package com.example.qxfw.backgroundManagement.controller;

import com.example.qxfw.backgroundManagement.service.UserManagerSecondService;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.Roles;
import com.example.qxfw.common.entity.UserTable;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.entity.xt.Users;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/4  星期二   23:51
 * @description : 此类用来 用户管理2
 ***************************************/
@ApiIgnore
@Api(tags = "后台管理-用户管理2")
@RestController
@CrossOrigin
@RequestMapping("/UserManagerController")
public class UserManagerController {

    @Autowired
    private UserManagerSecondService userManagerSecondService;

    @ApiOperation("查询所有用户")
    @RequestMapping(value = "selectUserTable")
    public SysResult selectUserTable() {
        List<UserTable> userTableList = this.userManagerSecondService.selectUserTable();
        return SysResult.success(userTableList);
    }

    @ApiOperation("查询所有权限")
    @RequestMapping("selectRoles")
    public SysResult selectRoles() {
        List<Roles> roles = this.userManagerSecondService.selectAll();
        return SysResult.success(roles);
    }

    @ApiOperation("查询所有站点信息")
    @RequestMapping(value = "selectStationInfo")
    public SysResult selectStationInfo() {
        List<StationInfo> stationInfos = this.userManagerSecondService.selectStationInfo();
        return SysResult.success(stationInfos);
    }

    @ApiOperation("添加用户")
    @RequestMapping(value = "addUsers")
    public SysResult addUsers(UserTable userTable, String stationid, String ownercity, String ownercounty, String ownertown, String ownervillage) {
        List<UserTable> userTables = this.userManagerSecondService.selectUserTableByUserName(userTable.getUsername());
        if (userTables.size() > 0) {
            return SysResult.fail("用户名已存在");
        }
        Integer insertUserTable = this.userManagerSecondService.insertUserTable(userTable);
        List<Users> usersList = this.userManagerSecondService.selectByUserNameAndType(userTable.getUsername(), userTable.getPermission());
        Users users = new Users();
        users.setUsername(userTable.getUsername());
        users.setPassword(userTable.getPassword());
        if (userTable.getPermission().contains("农业大户")) {
            users.setStationid(stationid);
            users.setOwnercity(ownercity);
            users.setOwnercounty(ownercounty);
            users.setOwnertown(ownertown);
            users.setOwnervillage(ownervillage);
        }
        if (usersList.size() > 0) {
            Integer updateUsers = this.userManagerSecondService.updateUsers(users);
        } else {
            Integer integer = this.userManagerSecondService.insertUsers(users);

        }
        return SysResult.success();

    }

    @ApiOperation("更新用户")
    @RequestMapping(value = "updateUsers")
    public SysResult updateUsers(UserTable userTable, String stationid, String ownercity, String ownercounty, String ownertown, String ownervillage) {
        List<UserTable> userTableList = this.userManagerSecondService.selectUserTableByUserName(userTable.getUsername());
        if (userTableList.size() > 0) {
            Integer updateUserTable = this.userManagerSecondService.updateUserTable(userTable);
        }
        Users users = new Users();
        if (userTable.getPermission().contains("农业大户")) {
            users.setStationid(stationid);
            users.setOwnercity(ownercity);
            users.setOwnercounty(ownercounty);
            users.setOwnertown(ownertown);
            users.setOwnervillage(ownervillage);
        }
        Integer updateUsers = this.userManagerSecondService.updateUsers(users);
        return SysResult.success();
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "deleteUser")
    public SysResult deleteUser(String username) {
        List<UserTable> userTableList = this.userManagerSecondService.selectUserTableByUserName(username);
        if (userTableList.size() > 0) {
            Integer deleteUserTable = this.userManagerSecondService.deleteUserTable(username);
            if (deleteUserTable > 0) {
                List<Users> usersList = this.userManagerSecondService.selectUsersByUsername(username);
                if (usersList.size() > 0) {
                    Integer deleteUsers = this.userManagerSecondService.deleteUsers(username);
                    if (deleteUsers <= 0) {
                        Integer insertUserTable = this.userManagerSecondService.insertUserTable(userTableList.get(0));
                    }
                    return SysResult.success();
                }
            }
        }
        return null;
    }

    @ApiOperation("迷糊查询")
    @RequestMapping(value = "querySelect")
    public SysResult querySelect(String starttime, String endtime, String username) {
        List<UserTable> userTableList = this.userManagerSecondService.querySelect(starttime, endtime, username);
        return SysResult.success(userTableList);
    }


}
